package learn.letcode.tree;

import java.util.ArrayList;

/**
 * 树的实现类
 * @param <T>
 */
public class TreeNode<T> extends Node {

    public TreeNode() {
    }

    public TreeNode(T nodeEntity) {
        super();
        super.nodeEntity = nodeEntity;
    }

    protected void addChildNode(Node<T> childNode) {
        childNode.setParentNode(this);
        if(childNodes == null) {
            childNodes = new ArrayList<Node<T>>();
        }
        this.childNodes.add(childNode);
    }

    protected void removeChildNode(Node<T> childNode) {
        if(childNodes != null) {
            childNodes.remove(childNode);
        }

    }

    public void display(Node<T> treeNode, int depth) {
        StringBuilder sb = new StringBuilder("");
        for(int i = 0; i < depth; i++) {
            sb.append("-");
        }
        if(treeNode.parentNode == null) {
            System.out.println(treeNode.getNodeEntity().toString());
        }
        for(Node<T> childNode : treeNode.getChildNodes()) {
            System.out.println(new String(sb) + childNode.getNodeEntity().toString());
            if(childNode.getChildNodes() != null) {
                display(childNode, depth + 2);
            }
        }

    }


}
